package com.banshuibao.bsb_android.activity;

import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.banshuibao.bsb_android.Constants;
import com.banshuibao.bsb_android.R;
import com.banshuibao.bsb_android.http.HttpResponse;
import com.banshuibao.bsb_android.http.HttpUtil;
import com.banshuibao.bsb_android.model.CommitAnswerModel;
import com.banshuibao.bsb_android.model.IfModel;
import com.banshuibao.bsb_android.util.GetLoginInfo;
import com.banshuibao.bsb_android.util.ImageUtil;
import com.banshuibao.bsb_android.util.IntentManager;
import com.banshuibao.bsb_android.util.MyEditText;
import com.banshuibao.bsb_android.util.QuickDoubleClickable;
import com.banshuibao.bsb_android.util.TimeUtil;
import com.banshuibao.bsb_android.util.XToast;

public class CommitAnswerActivity extends BaseActivity implements View.OnClickListener {

    private ImageView back;
    private TextView commit;
    private ImageView headImage;
    private TextView name_tv;
    private TextView content_tv;
    private ImageView imageOne, imageTwo, imageThree;
    private TextView date_tv;
    private MyEditText myEditText;
    private String imgs;
    private String[] split;
    private String uuid;
    private int flag;
    private String role;
    private long date;
    private String name;
    private String headImg;
    private String content;
    private long createDate;
    private LinearLayout linearLayout;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_commit_answer);
        initView();
        getData();
    }

    private void initView() {
        back = (ImageView) findViewById(R.id.back_iv);
        back.setVisibility(View.GONE);
        setTitleText("回答问题");
        commit = (TextView) findViewById(R.id.menu);
        commit.setVisibility(View.VISIBLE);
        commit.setText("提交");
        commit.setOnClickListener(this);

        headImage = (ImageView) findViewById(R.id.header_iamge);
        name_tv = (TextView) findViewById(R.id.name);
        content_tv = (TextView) findViewById(R.id.content);
        imageOne = (ImageView) findViewById(R.id.image_one);
        imageTwo = (ImageView) findViewById(R.id.image_two);
        imageThree = (ImageView) findViewById(R.id.image_three);
        linearLayout = (LinearLayout) findViewById(R.id.liner);
        date_tv = (TextView) findViewById(R.id.date);
        myEditText = (MyEditText) findViewById(R.id.quick_edit);
        imageOne.setOnClickListener(this);
        imageTwo.setOnClickListener(this);
        imageThree.setOnClickListener(this);
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.menu:
                if (QuickDoubleClickable.isDoubleClick())
                    return;
                String content = myEditText.getText().toString().trim();
                if (!TextUtils.isEmpty(content)) {
                    showProgressDialog();
                    setPost(content);
                } else {
                    XToast.showToast(this, "请输入答案");
                }
                break;
            case R.id.image_one:
                IntentManager.goBitmapShowActivity(CommitAnswerActivity.this, split, 0);
                break;
            case R.id.image_two:
                IntentManager.goBitmapShowActivity(CommitAnswerActivity.this, split, 1);
                break;
            case R.id.image_three:
                IntentManager.goBitmapShowActivity(CommitAnswerActivity.this, split, 2);
                break;
        }
    }

    private void setPost(String content) {
        CommitAnswerModel model = new CommitAnswerModel();
        model.setContent(content);
        if (!TextUtils.isEmpty(uuid)) {
            model.setUuid(uuid);
        }
        HttpUtil.doPost(Constants.Url.COMMIT_CONSULTTATION, model, new HttpResponse(this) {
            @Override
            public void onResult(Object obj) {
//                String s = (String) obj;
//                LogUtil.e(s);
                cancelProgressDialog();
                if (flag != 0 && flag == 1) {
                    finish();
                } else if (flag != 0 && flag == 2) {
                    IntentManager.goUserHomeActivity(CommitAnswerActivity.this);
                    finish();
                }
            }

            @Override
            public void responseEnd() {
                super.responseEnd();
                cancelProgressDialog();
            }

            @Override
            public void onError(String code, String msg) {
                super.onError(code, msg);
                if (TextUtils.equals(code, Constants.Code.FORCE_ANSWER)) {
                    XToast.showToast(context, msg);
                    finish();
                }
            }
        });
    }

    @Override
    public void onBackPressed() {

    }

    private void getData() {
        //AnswerTobeActivity 跳转结果
        role = GetLoginInfo.getLoginInfoInstance(this).readUser().getRole();
        uuid = getIntent().getStringExtra("uuid");
        headImg = getIntent().getStringExtra("headImage");
        name = getIntent().getStringExtra("name");
        content = getIntent().getStringExtra("content");
        createDate = getIntent().getLongExtra("time", 0);
        imgs = getIntent().getStringExtra("imgs");
        flag = getIntent().getIntExtra("flag", 0);

        //MainActivity 跳转结果
        IfModel.DataBean item = (IfModel.DataBean) getIntent().getSerializableExtra("item");
        if (item != null) {
            name = item.getName();
            headImg = item.getHeadImg();
            content = item.getContent();
            createDate = item.getCreateDate();
            imgs = item.getImgs();
            uuid = item.getUuid();
        }
        setData();
    }

    private void setData() {
        if (!TextUtils.isEmpty(headImg)) {
            ImageUtil.loadImageHead(this, headImg, headImage);
        }
        name_tv.setText(name);
        content_tv.setText(content);
        String time = null;
        if (createDate != 0) {
            time = TimeUtil.format(createDate);
        } else {
            time = "";
        }
        date_tv.setText(time);
        if (!TextUtils.isEmpty(imgs)) {
            split = imgs.split(",");
            if (split.length > 0) {
                if ("null".equals(split[0]))
                    return;
                if (split.length == 1) {
                    ImageUtil.loadImage(CommitAnswerActivity.this, split[0], imageOne);
                    imageTwo.setVisibility(View.GONE);
                    imageThree.setVisibility(View.GONE);
                } else if (split.length == 2) {
                    ImageUtil.loadImage(CommitAnswerActivity.this, split[0], imageOne);
                    ImageUtil.loadImage(CommitAnswerActivity.this, split[1], imageTwo);
                    imageThree.setVisibility(View.GONE);
                } else if (split.length == 3) {
                    ImageUtil.loadImage(CommitAnswerActivity.this, split[0], imageOne);
                    ImageUtil.loadImage(CommitAnswerActivity.this, split[1], imageTwo);
                    ImageUtil.loadImage(CommitAnswerActivity.this, split[2], imageThree);
                }
            }
        } else {
            imageTwo.setVisibility(View.GONE);
            imageThree.setVisibility(View.GONE);
            imageOne.setVisibility(View.GONE);
            linearLayout.setVisibility(View.GONE);
        }
    }
}
